#include <zeosfat.h>

unsigned int inited = 0;

int initZeosFat(void)
{
  int i,j;
  
  if (inited) return -1;
  
  first_free_block = 0;
  
  for (i=0; i<MAX_BLOCKS-1; ++i) FAT[i]=i+1;
  FAT[MAX_BLOCKS-1] = EOF;
  
  for (i = 0; i < MAX_BLOCKS;++i)
  {
    for (j = 0; j < BLOCK_SIZE;++j)
      HD[i][j] = 0;
  }
  inited = 1;
  return 0;
}

int get_free_block()
{
  int value = first_free_block;
  if (value == EOF) return -1;
  first_free_block = FAT[value];
  return value;
}

void free_blocks(int fb)
{
  int oldfb = first_free_block;
  first_free_block = fb;
  while (FAT[fb] != EOF) fb = FAT[fb];
  FAT[fb] = oldfb;
}
